package edu.nwpu.zya.coreops.modules.alert.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import edu.nwpu.zya.coreops.common.utils.Constant;
import edu.nwpu.zya.coreops.common.utils.DateUtils;
import edu.nwpu.zya.coreops.modules.alert.dao.HistoryStatisticDao;
import edu.nwpu.zya.coreops.modules.alert.service.StatisticService;
import edu.nwpu.zya.coreops.modules.alert.vo.AlertLevelCountVo;
import edu.nwpu.zya.coreops.modules.alert.vo.MetricVo;
import edu.nwpu.zya.coreops.modules.alert.vo.OccurTrendVo;
import edu.nwpu.zya.coreops.modules.alert.vo.RecoveryTrendVo;
import edu.nwpu.zya.coreops.modules.dashboard.entity.AlertTrendEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("statisticService")
public class StatisticServiceImpl implements StatisticService {
    @Autowired
    private HistoryStatisticDao historyStatisticDao;

    @Override
    public List<OccurTrendVo> getOccurTrendByParams(Map<String, Object> params) {
        Long userId = (Long) params.get("userId");
        // timePeriod单位为分钟
        Integer timePeriod = Integer.parseInt(String.valueOf(params.get("timePeriod")));

        Date endTime = new DateTime();
        Date startTime = DateUtils.addDateMinutes(endTime, 0 - timePeriod);

        String type = String.valueOf(params.get("type"));
        String instanceId = String.valueOf(params.get("instanceId"));
        String metricId = String.valueOf(params.get("metricId"));
        if (userId.intValue() == Constant.SUPER_ADMIN) {
            userId = null;
        }

        Integer timeGap = getTimeGap(timePeriod);

        return historyStatisticDao.getOccurTrend(userId, startTime, endTime, timeGap, type, instanceId,metricId);
    }

    @Override
    public List<RecoveryTrendVo> getRecoveryTrendByParams(Map<String, Object> params) {
        Long userId = (Long) params.get("userId");
        // timePeriod单位为分钟
        Integer timePeriod = Integer.parseInt(String.valueOf(params.get("timePeriod")));

        Date endTime = new DateTime();
        Date startTime = DateUtils.addDateMinutes(endTime, 0 - timePeriod);

        String type = String.valueOf(params.get("type"));
        String instanceId = String.valueOf(params.get("instanceId"));
        String metricId = String.valueOf(params.get("metricId"));
        if (userId.intValue() == Constant.SUPER_ADMIN) {
            userId = null;
        }
        Integer timeGap = getTimeGap(timePeriod);


        return historyStatisticDao.getRecoveryTrend(userId, startTime, endTime, timeGap, type, instanceId,metricId);
    }

    @Override
    public List<AlertLevelCountVo> getLevelCountByParams(Map<String, Object> params) {
        Long userId = (Long) params.get("userId");
        // timePeriod单位为分钟
        Integer timePeriod = Integer.parseInt(String.valueOf(params.get("timePeriod")));
        Date endTime = new DateTime();
        Date startTime = DateUtils.addDateMinutes(endTime, 0 - timePeriod);

        String type = String.valueOf(params.get("type"));
        String instanceId = String.valueOf(params.get("instanceId"));
        String metricId = String.valueOf(params.get("metricId"));

        if (userId.intValue() == Constant.SUPER_ADMIN) {
            userId = null;
        }
        return historyStatisticDao.getLevelStatistic(userId, startTime, endTime, type, instanceId,metricId);
    }

    @Override
    public List<MetricVo> getMetricCountByParams(Map<String, Object> params) {
        Long userId = (Long) params.get("userId");
        // timePeriod单位为分钟
        Integer timePeriod = Integer.parseInt(String.valueOf(params.get("timePeriod")));
        Date endTime = new DateTime();
        Date startTime = DateUtils.addDateMinutes(endTime, 0 - timePeriod);

        String type = String.valueOf(params.get("type"));
        String instanceId = String.valueOf(params.get("instanceId"));
        String metricId = String.valueOf(params.get("metricId"));

        if (userId.intValue() == Constant.SUPER_ADMIN) {
            userId = null;
        }
        return historyStatisticDao.getMetricStatistic(userId, startTime, endTime, type, instanceId,metricId);
    }

    /**
     * 获取当日的告警趋势数据
     * @param userId
     * @return
     */
    @Override
    public List<AlertTrendEntity> getTodayAlertTrend(Long userId) {
        List<AlertTrendEntity> result=new ArrayList<>();
        List<String> todayTimes=new ArrayList<String>();
        Date now = new Date();
        Date start = DateUtil.beginOfDay(now).toJdkDate();
        int hour = DateUtil.hour(now, true);
        for (int i = 0; i <= hour; i++) {
           todayTimes.add(DateUtils.format(DateUtils.addDateHours(start,i),DateUtils.DATE_TIME_PATTERN));
        }
        if(userId.intValue()==Constant.SUPER_ADMIN){
            todayTimes.forEach(time->{
                result.add(historyStatisticDao.getHourAlertTrend(time));
            });
        }else {
            todayTimes.forEach(time->{
                result.add(historyStatisticDao.getHourAlertTrendByUserId(userId,time));
            });
        }

        return result;
    }

    private Integer getTimeGap(Integer timePeriod) {
        if (timePeriod / (6 * 60) == 0) {
            return 10;
        } else if (timePeriod / (6 * 60) == 1) {
            return 15;
        } else if (timePeriod / (6 * 60) == 2) {
            return 30;
        } else {
            return 60;
        }
    }
}
